﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace shortcutCreator
{
    class Program
    {
        static void Main(string[] args)
        {
            String targetExe = String.Empty;
            String workingDirectory = String.Empty;
            String Description = String.Empty;
            String IconPath = String.Empty;
            String saveLocation = String.Empty;

            if (args.Length == 4 || args.Length == 5)
            {
                targetExe = args[0];
                workingDirectory = args[1];
                if (workingDirectory.ToUpper().Contains("CWD")) { workingDirectory = workingDirectory.Replace("CWD", System.IO.Directory.GetCurrentDirectory()); }
                Description = args[2];
                saveLocation = args[3];
                if (saveLocation.ToUpper().Contains("ALLUSERSDESKTOP")) { String temp = GetAllUsersDesktopFolderPath(); saveLocation = saveLocation.ToUpper().Replace("ALLUSERSDESKTOP", temp); }
                if (args.Length == 5)
                {
                    IconPath = args[4];
                }
                createShortcut(targetExe, workingDirectory, Description, IconPath, saveLocation);
            }
            else
            {
                Console.WriteLine("targetExe workingDirectory Description savelocation [iconPath]\ncan use String AllUsersDesktop to reference desktop for save location");
            }
        }
        private static void createShortcut(String targetExe, String workingDirectory, String Description, String IconPath, String saveLocation) 
        { 
            using (ShellLink shortcut = new ShellLink()) 
            { 
                shortcut.Target = targetExe; 
                shortcut.WorkingDirectory = workingDirectory; 
                shortcut.Description = Description; 
                shortcut.DisplayMode = ShellLink.LinkDisplayMode.edmNormal;
                if (!IconPath.Equals(String.Empty)) { shortcut.IconPath = IconPath; }
                shortcut.Save(saveLocation);
            } 
        }

        [DllImport("shfolder.dll", CharSet = CharSet.Auto)]
        private static extern int SHGetFolderPath ( IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath );
        private const int MAX_PATH = 260;
        private const int CSIDL_COMMON_DESKTOPDIRECTORY = 0x0019;
        public static string GetAllUsersDesktopFolderPath ( )
        {

            StringBuilder sbPath = new StringBuilder(MAX_PATH);

            SHGetFolderPath(IntPtr.Zero, CSIDL_COMMON_DESKTOPDIRECTORY, IntPtr.Zero, 0, sbPath);

            return sbPath.ToString();

        }
    }
}
